/**
 * 登录控制器
 * 
 * @param $scope
 * @param $http
 * @returns
 */

function LoginCtrl($scope, $http, $state) {
	$scope.wrong = false;
	$scope.user = null;
	$scope.login = function() {

		$http({
			url : 'login',
			method : 'get',
			params : $scope.user
		}).success(function(response) {
			if (response == "success") {
				$state.go('index');
			} else {
				$scope.wrong = true;
			}
		})
	}

}

function SignUpCtrl($scope, $http, $state, $interval) {
	// 代码验证时间
	$scope.description = "获取验证码";
	var second = "59";
	// 发送验证码
	var timeHandler;

	$scope.sendCode = function() {
		$scope.phone=$scope.user.telephone
		$http({
			url : 'code',
			method : 'post',
			params :{ 
				phone:$scope.phone
				}
			
		}).success(function(response) {
			if (response == "success") {
				if (timeHandler) {
					return;
				}
				timeHandler = $interval(function() {
					if (second <= 0) {
						$interval.cancel(timeHandler);
						second = 59;
						$scope.description = "获取验证码";
					} else {
						$scope.description = second + " 秒后重发";
						second--;
					}
				})
			} else {
				alert("手机号输入错误");
			}
		})
	}
	$scope.submit = function() {
		if ($scope.user.password == $scope.password1) {
			$http({
				url : "signup",
				method : 'post',
				params : $scope.user
			}).success(function(response) {
				if (response == "success") {
					$state.go('login');
				} else {
					alert("注册失败，请重试！");
				}
			})
		}else{
			alert("两次密码不一致");
		}

	}
}